package com.zhiqianbao.activity;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

import com.google.gson.Gson;
import com.zhiqianbao.R;
import com.zhiqianbao.entity.Result;
import com.zhiqianbao.utils.Constants;
import com.zhiqianbao.utils.HttpUtils;

import java.util.HashMap;
import java.util.Map;
import cn.trinea.android.common.util.StringUtils;

/**
 * Created by liang on 2016/1/23.
 */
public class PayPhoneConfirmActivity extends BaseActivity implements View.OnClickListener {
    /**
     * 手机验证码
     */
    EditText textViewPayBankPhoneContent;
    /**
     * 确认支付
     */
    Button buttonConfirm;

    String dataStr;
    Gson gson = new Gson();
    Map<String, String> data;

    Handler payConfirmHandler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            closeWaitDialog();
            switch (msg.what) {
                case RESULT_CANCELED :
                    showShortToast1(Constants.HTTP_ERROR_MESSAGE);
                    break;
                case RESULT_OK :
                    Result<HashMap> data = (Result<HashMap>) msg.obj;
                    if(data!=null) {
                        if (data.getCode().equals(Constants.DEFAULT_SUCCESS_CODE)) {
                            Intent mainIntent = new Intent(PayPhoneConfirmActivity.this, MainActivity.class);
                            mainIntent.putExtra("type", "paySuccess");
                            mainIntent.putExtra("tabPosition", 1);
                            startActivity(mainIntent);
                            PayPhoneConfirmActivity.this.finish();
                            showShortToast1(data.getMsg());
                        } else {
                            showShortToast1(data.getMsg());
                        }
                    }
                    break;
            }

        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_pay_phone_confirm);
        init();
        initView();
    }

    private void initView() {
         textViewPayBankPhoneContent=(EditText)findViewById(R.id.textView_pay_bank_phone_content);
         buttonConfirm=(Button)findViewById(R.id.button_confirm);

        dataStr = (String) getIntent().getSerializableExtra("dataStr");
        data = gson.fromJson(dataStr, HashMap.class);

        buttonConfirm.setOnClickListener(this);

    }

    @Override
    public void onClick(View v) {
        if(R.id.button_confirm==v.getId()){
                if (StringUtils.isBlank(textViewPayBankPhoneContent.getText().toString())) {
                    showShortToast1("请填入正确的手机验证码！");
                } else {
                    //请求支付接口
                    showWaitDialog();
                    new Thread(new Runnable() {
                        @Override
                        public void run() {
                            Message message = new Message();
                            Map<String, String> params = new HashMap<>();
                            params.put("token", Constants.LOGIN_USER != null ? Constants.LOGIN_USER.getToken() : "");
                            params.put("userId", Constants.LOGIN_USER != null ? Constants.LOGIN_USER.getId() + "" : "");
                            params.put("bankCard", data.get("bankCard"));
                            params.put("validCode", textViewPayBankPhoneContent.getText().toString());
                            params.put("dynNumToken", data.get("dynNumToken"));
                            //Long userId,
                            //String bankCard,
                           // String validCode,
                            //String dynNumToke
                            String result = HttpUtils.httpPostString(Constants.API_ROOT + "/payment/quickPay", params, PayPhoneConfirmActivity.this);
                            if (StringUtils.isBlank(result)) {
                                message.what = RESULT_CANCELED;
                            } else {
                                message.what = RESULT_OK;
                                Result<HashMap> data = Result.fromJson(result, HashMap.class);
                                message.obj = data;
                            }
                            payConfirmHandler.sendMessage(message);
                        }
                    }).start();


                }
        }
    }
}
